package com.zjk.controller.user;

import com.github.pagehelper.PageInfo;
import com.zjk.entity.Result;
import com.zjk.pojo.SysUser;
import com.zjk.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private ISysUserService userService;
    //去登录页面
    @RequestMapping("/to_login")
    public  String toLogin(){

        return "login";
    }
//登录验证
    @RequestMapping("/login")
    public  String Login(SysUser sysUser, Model model, HttpSession httpSession){
        System.out.println("收到数据");
        System.out.println(sysUser);
        if (sysUser==null || sysUser.getUsername()==null
                || sysUser.getUsername().trim().equals("")
                || sysUser.getPassword()==null
                || sysUser.getPassword().trim().equals("")){

            model.addAttribute("msg","请输入用户名或密码");

            return "login" ;
        }
        SysUser sysUser1 = userService.getUserNyUsernameAndPwd(sysUser);
            if(sysUser1!=null){
                httpSession.setAttribute("USER_SESSION",sysUser1);
                model.addAttribute("name",sysUser1.getName());

                return "main";
            }else {
                model.addAttribute("msg","用户名或密码错误");
                return "login" ;
            }


    }
    //去主页面
    @RequestMapping("/main")
    public String main(HttpSession httpSession,Model model){
        SysUser userSession = (SysUser)httpSession.getAttribute("USER_SESSION");
        model.addAttribute(userSession);
        return "main";
    }
    //去首页
    @RequestMapping("/index")
    public String index(HttpSession httpSession,Model model){

        return "index";
    }
//用户列表
    @RequestMapping("/userList")
    public  String userList(String username,Model model,
                            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize",defaultValue = "2")   Integer pageSize
                            ){

        List<SysUser> allUsers = userService.getAllUsers(username,pageNum,pageSize);
        PageInfo<Object> pageInfo = new PageInfo<>(allUsers);
        model.addAttribute("userList",allUsers);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("username",username);
        return "user_list";
    }


    //新增用户列表
    @RequestMapping("/addUser")
    @ResponseBody
    public Result addUser(SysUser sysUser){
        Result<Object> result = new Result<>();

        if (sysUser!=null && sysUser.getUsername()!=null
                && !sysUser.getUsername().trim().equals("")
                && sysUser.getPassword()!=null
                && !sysUser.getPassword().trim().equals("")){

            SysUser user = userService.getUserByUsername(sysUser.getUsername());
            System.out.println(user);
            if (user!=null){
                result.setMsg("用户已经存在");
                result.setSuccess(false);
                return result;
            }
            int i = userService.addUser(sysUser);
            System.out.println(i);
            if (i>0){
                result.setMsg("新增成功");
                result.setSuccess(true);
                return result;
            }else {
                result.setMsg("新增失败");
                result.setSuccess(false);
                return result;
            }
        }else {
            System.out.println("请输入用户名密码");
            result.setMsg("请输入用户名密码");
            result.setSuccess(false);
            return result;
        }
    }

    @RequestMapping("/getUserById")
    @ResponseBody
    public  Result getUserById(Integer id){
        Result<Object> result = new Result<>();
        if (id!=null){
            SysUser user = userService.getUserById(id);

            if(user!=null){
                result.setData(user);
                result.setMsg("成功");
                result.setSuccess(true);
                return result;
            }else {
                result.setMsg("没有此用户");
                result.setSuccess(false);
                return result;
            }
        }

        return result;
    }


    //修改用户列表
    @RequestMapping("/editUser")
    @ResponseBody
    public Result editUser(SysUser sysUser){
        Result<Object> result = new Result<>();

        if (sysUser!=null && sysUser.getUsername()!=null
                && !sysUser.getUsername().trim().equals("")
                && sysUser.getId()!=null
        ){
            int i = userService.editUser(sysUser);
            System.out.println(i);
            if (i>0){
                result.setMsg("修改成功");
                result.setSuccess(true);
                return result;
            }else {
                result.setMsg("修改失败");
                result.setSuccess(false);
                return result;
            }
        }
        return result;
    }
    @RequestMapping("/editUserPassword")
    @ResponseBody
    public Result editUserPassword(SysUser sysUser,HttpSession httpSession){
        SysUser userSession = (SysUser) httpSession.getAttribute("USER_SESSION");
        sysUser.setId(userSession.getId());
        Result<Object> result = new Result<>();

        if (sysUser!=null && sysUser.getPassword()!=null
                && !sysUser.getPassword().trim().equals("")
        ){
            int i = userService.editUser(sysUser);
            System.out.println(i);
            if (i>0){
                result.setMsg("修改成功");
                result.setSuccess(true);
                return result;
            }else {
                result.setMsg("修改失败");
                result.setSuccess(false);
                return result;
            }
        }
        return result;
    }

    //删除用户列表
    @RequestMapping("/delUser")
    @ResponseBody
    public Result delUser(Integer id){
        Result<Object> result = new Result<>();
        int i = userService.delById(id);
        if(i>0){
            result.setMsg("删除成功");
            result.setSuccess(true);
            return result;
        }else {
            result.setMsg("新增失败");
            result.setSuccess(false);
            return result;
        }
    }


    //注销
    @RequestMapping("/exitUser")
    public String exitUser(HttpSession httpSession){
            httpSession.invalidate();
            return "login";
    }


    //批量删除用户列表
    @RequestMapping("/delAllUser")
    @ResponseBody
    public Result delAllUser(String ids){
        Result<Object> result = new Result<>();
        System.out.println("收到删除数据");
        System.out.println(ids);
        String[] strs =ids.split(",");
        List<Integer> id = new ArrayList<>();
        System.out.println(strs.length);
        for(int i = 0;i<strs.length; i++){
            id.add(Integer.parseInt(strs[i].trim()));
//           ids.add(Integer.valueOf(strs[i]));
//            id.add(strs[i].length());
        }

        System.out.println(id.size());
        int i = userService.delAll(id);
        if(i>0){
            result.setMsg("删除成功");
            result.setSuccess(true);
            return result;
        }else {
            result.setMsg("删除失败");
            result.setSuccess(false);
            return result;
        }

    }}
